package com.vmove.childtrain.common;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.vmove.childtrain.utils.LogUtils;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * Created by gxd on 2017/11/22.
 * @Description:fragment基类 是否缓存view ，默认缓存
 */

public abstract class BaseFragment extends Fragment {
    private View rootView;
    public Context mContext;
    private boolean isCacheView = true;//是否缓存view
    Unbinder unbinder;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
        LogUtils.d(this.getClass().getSimpleName() + "---onAttach");
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isCacheView = setISCacheView();
        LogUtils.d(this.getClass().getSimpleName()+"  onCreated1");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        LogUtils.d(this.getClass().getSimpleName() + "onCreateView");
        if (null != rootView) {
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (parent != null) {
                parent.removeView(rootView);
            }
        }else {
            rootView = loadView(inflater);
        }
        unbinder = ButterKnife.bind(this, rootView);
        LogUtils.d(this.getClass().getSimpleName()+"  onCreatedView2");
        bindView(rootView);
        return rootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        LogUtils.d(this.getClass().getSimpleName()+"  onViewCreated3");
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        LogUtils.d(this.getClass().getSimpleName()+"  onActivityCreated4");
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        rootView=null;
        LogUtils.i(this.getClass().getSimpleName() + "---onDestroyView");
    }

    public void setSerialReceivedData(byte[] data){

    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    /**
     * 子类必须实现，载入自己的布局文件
     * 如果缓存view，则只会执行一次
     * @param inflater
     * @return
     */
    protected abstract View loadView(LayoutInflater inflater);

    /**
     * 子类必须实现，每次显示fragment时更新view
     * 不管缓存与否，每次都执行
     * @param rootView
     */
    protected abstract void bindView(View rootView);

    /**
     * 默认缓存view，overwrite此方法，返回false，不缓存view
     * @return 是否缓存view
     */
    public boolean setISCacheView() {
        return true;
    }

    public View getView() {
        return rootView;
    }

    /**
     * 在该方法中才可以使用mContext
     */
    public void loadData() {
    }

    /**
     * 修改titleBar标题
     *
     * @param s
     */
    public void setActivityTitle(String s) {
        if (null != mContext) {
            BaseActivity activity = (BaseActivity) mContext;
            activity.setTitle(s);
        }
    }
}
